package com.suse.warmstill.controller;

import com.baomidou.mybatisplus.core.toolkit.ObjectUtils;
import com.suse.warmstill.common.exception.BusinessException;
import com.suse.warmstill.common.model.CallResult;
import com.suse.warmstill.common.model.ListPageModel;
import com.suse.warmstill.mapper.CommentMapper;
import com.suse.warmstill.service.CommentService;
import com.suse.warmstill.vo.CommentPostVo;
import com.suse.warmstill.vo.CommentRootVo;
import com.suse.warmstill.vo.CommentVo;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiOperation;
import org.springframework.validation.annotation.Validated;
import org.springframework.web.bind.annotation.*;

import javax.annotation.Resource;
import java.util.List;
import java.util.Objects;

/**
 * @author 黄靖
 * @author 2023-02-04 15:37
 * description:
 **/
@Validated
@RestController
@RequestMapping("/comment")
@Api(tags = "wx--》的评论的")
public class CommentApi {

    @Resource
    private CommentService commentService;

    @Resource
    private CommentMapper commentMapper;

    @PostMapping("/wx/sava")
    @ApiOperation("发布评论")
    public CallResult<CommentVo> save(@RequestBody @Validated CommentPostVo commentPostVo){
        if(ObjectUtils.isNull(commentPostVo.getMessage())){
            throw new BusinessException(111,"message不能为空");
        }

      return    commentService.save(commentPostVo);

    }


    @GetMapping("/wx/listCommentsByRoot")
    @ApiOperation("根据单个 一级评论root查询所有的二级评论")
    public CallResult<CommentRootVo> listComments(@RequestParam String rootId){

        final CommentRootVo commentRootVo = commentService.listComments(rootId);
        return CallResult.success(commentRootVo);
    }


    @GetMapping("/wx/listAllComments")
    @ApiOperation("根据文章id拿到所有的评论")
    public CallResult<ListPageModel<CommentRootVo>> listAllComments(
            @RequestParam("articleId") Long articleId,
            @RequestParam("size")int size,
            @RequestParam("current")int current
            ){

        ListPageModel<CommentRootVo> list = commentService.listAllComments(articleId,size,current);


        return CallResult.success(list);
    }


    @ApiOperation("删除评论")
    @GetMapping
    public CallResult delete(Integer id){
        return CallResult.success(commentMapper.deleteById(id));
    }














}
